package com.springboot.film.dao;

import com.springboot.film.entity.looktime;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "looktimeDao")
public interface looktimeDao {

    //观影时长：looktime

    /**
     * 显示总数量
     * @return
     */
    @Select("select count(*) from looktime")
    int getCount();

    /**
     * 全部分页显示
     * @return
     */
    @Select("select a.*,b.name as uname,c.name as fname,round(a.time) as time_temp" +
            " from looktime a,userinfo b,films c where a.uid=b.id and a.fid=c.id limit #{pos},#{size}")
    List<looktime> findByPage(@Param("pos")int pos, @Param("size")int size);

    /**
     * 显示搜索的总数量
     * @return
     */
    @Select("select count(*) from looktime a,userinfo b,films c where " +
            "a.uid=b.id and a.fid=c.id and ${key} like concat(concat('%',#{name}),'%')")
    int getsearchCount(@Param("key") String key, @Param("name")String name);

    /**
     * 搜索并分页显示
     * @return
     */
    @Select("select a.*,b.name as uname,c.name as fname,round(a.time) as time_temp" +
            " from looktime a,userinfo b,films c where a.uid=b.id and a.fid=c.id and " +
            "${key} like concat(concat('%',#{name}),'%') limit #{pos},#{size}")
    List<looktime> findsearchByPage(@Param("key")String key,@Param("name")String name,@Param("pos")int pos, @Param("size")int size);

    /**
     * 新增
     */
    @Insert("INSERT INTO looktime(uid,fid,time) " +
            "VALUES(#{uid},#{fid},round(#{time_temp}))")
    void houtaiadd(looktime looktime);
    @Insert("INSERT INTO looktime(uid,fid,time) " +
            "VALUES(#{uid},#{fid},round(#{time_temp}))")
    void add(@Param("uid")int uid,@Param("fid")int fid,@Param("time_temp")int time_temp);

    /**
     * 删除
     */
    @Delete("DELETE FROM looktime WHERE ID=#{id}")
    void delete(@Param("id")int id);

    /**
     * 修改(先找到并显示，再修改)
     */
    @Select("select *,round(time) as time_temp from looktime where id=#{id}")
    List<looktime> getUserone(@Param("id")int id);

    @Update("update looktime set uid=#{uid},fid=#{fid},time=round(#{time_temp}) where id=#{id}")
    void update(looktime looktime);

    @Select("select max(a.time) from looktime a,userinfo b,films c where " +
            "a.uid=b.id and a.fid=c.id and a.uid=#{uid} and a.fid=#{fid}")
    int getmaxtime(@Param("uid")int uid,@Param("fid")int fid);

    @Select("select count(id) from looktime where uid=#{uid} and fid=#{fid}")
    int getcountid(@Param("uid")int uid,@Param("fid")int fid);

    @Select("select id from looktime where uid=#{uid} and fid=#{fid}")
    int getmaxid(@Param("uid")int uid,@Param("fid")int fid);

    @Update("update looktime set time=time+#{time_temp} where id=#{id}")
    void updatelooktime(@Param("time_temp")int time_temp,@Param("id")int id);

}
